function ajax(jsonObj){//jsonObj对象格式有要求
    //1.create xhr
    let xhr=createXHR(jsonObj.callback);
    //2.create connection
    let requestUrl=jsonObj.requestUrl;
    let requestParams=jsonObj.requestParams;
    let requestType=jsonObj.requestMethod;
    let url=requestType=="GET"?`${requestUrl}/${requestParams}`:requestUrl;
    xhr.open(requestType,url,true);
    if(requestType=="POST"){
        xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    }
    //3.send request
    xhr.send(requestType=="POST"?requestParams:null);
}

function ajaxGet(url,params,callback) {

    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange=function () {
        if (xhr.readyState==4&&xhr.status==200){
            callback(xhr.responseText);
        }
    }
    xhr.open("GET",`${url}/${params}`,true);
    xhr.send(null);
}

function ajaxDelete(url,params,callback) {

    let xhr=createXHR(callback);
    //xhr.open("DELETE",url,true);
    xhr.open("DELETE",`${url}/${params}`,true);
    xhr.send(params);
}

function ajaxPut(url,params,callback) {

    let xhr=createXHR(callback);
    xhr.open("PUT",url,true);
    xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded")
    xhr.send(params);
}

function ajaxPost(url,params,callback) {

    let xhr=createXHR(callback);
    xhr.open("POST",url,true);
    xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded")
    xhr.send(params);
}

function ajaxPostJson(url,params,callback) {

    let xhr=createXHR(callback);
    xhr.open("POST",url,true);
    xhr.setRequestHeader("Content-type","application/json")
    xhr.send(JSON.stringify(params));
}

function createXHR(callback) {
    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange=function () {
        if (xhr.readyState==4&&xhr.status==200){
            callback(xhr.responseText);
        }
    }
    return xhr;
}